package com.example.demo2.dao;

import java.io.*;

/**
 * @description This class is for writting json data
 * @version 2.0
 */
public class JsonWriter {
    public static void saveDataToFile(String fileName, String data) {
        BufferedWriter writer = null;
        File file = new File("d:\\"+ fileName + ".json");
        //如果文件不存在，则新建一个
        if(!file.exists()){
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //写入
        try {
            writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file,false), "UTF-8"));
            writer.write(data);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if(writer != null){
                    writer.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
